"""
翻页依赖于上一页的参数  如果某个之母的页数不对
删除该字母的所有html网页 重新下载
"""
import os
import shutil
import sqlite3
import sys

from bs4 import BeautifulSoup
from xjlibrary.mysqlmoudel.simple.mysqlclient import MySqlDbConnect, SelctSqlFromDB
from xjlibrary.mysqlmoudel.simple.sqlite3cloent import sqliteEscape
from xjlibrary.tools.BaseFile import BaseFile
from xjlibrary.tools.filename import getDirAllFiles, get_filename_not_extsep

curPath = BaseFile.get_path_absolute(__file__)
TopPath = BaseFile.get_top_path(curPath, -2)
chapter_path = BaseFile.getNewPath(TopPath, "download", "tianfang", "download", "chapter")
out_path = BaseFile.getNewPath(TopPath, "download", "tianfang", "download", "chapter2")
results = []
# 数据库链接
def MajorDbConnect():
    return MySqlDbConnect(curPath, "db.ini")

# 数据库链接


def InsertSqlToDB3(sql):
    try:
        conn = sqlite3.connect('mirrordqlibtingbookvideo.db3')
        cur = conn.cursor()
        row = cur.execute(sql)
        if row.rowcount != 1:
            print(sql)
            print("请检查  按照逻辑应该不存在这种情况")
            sys.exit(-1)
        conn.commit()
        cur.close()
        conn.close()
    except Exception as e:
        raise e


def parahtml(filePath):
    global ListSql
    print(filePath)
    charName = get_filename_not_extsep(filepath)
    id = charName.split("_")[-1]
    # if id not in dicts:
    #     return
    soup = BeautifulSoup(open(filePath, 'r', encoding='utf-8'), "lxml")
    # P_all_tag = soup.find_all("p", attrs={"style": "line-height: 150%"})
    table = soup.find("table")
    try:
        print("################")
        p_strings = "".join(table.stripped_strings)
        if p_strings == "":
            raise Exception
        if p_strings.find("暂无相关简介!") != -1:
            p_strings = ""
    except:
        print("************************")
        body_tag = soup.find("body") # ,attrs={"bgcolor":"#FFFFFF", "text":"#000000"}
        if body_tag:
            p_strings = "".join(body_tag.stripped_strings)
        else:
            p_strings=""
    print(p_strings)
    sql = "update `modify_title_info_zt` set `description`='{}' where `rawid`='{}'".format(sqliteEscape(p_strings),id)
    print(sql)
    InsertSqlToDB3(sql)
    if not os.path.exists(out_path):
        os.makedirs(out_path)
    shutil.move(filePath, out_path)


def SelectNull():
    sql = "select `rawid`,`description` from `modify_title_info_zt` where `description`=''"
    conn = sqlite3.connect('mirrordqlibtingbookvideo.db3')
    rows = SelctSqlFromDB(sql, conn)
    conn.close()
    return rows

if __name__ == "__main__":
    # dicts = []
    # for row in SelectNull():
    #     dicts.append(row[0])
    for filepath in getDirAllFiles(chapter_path):
        parahtml(filepath)

